package com.jar.exercise.网络编程.BIO和NIO和AIO;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * BIO（Blocking-IO）
 * 同步阻塞的
 */

class Server1 {

	@SuppressWarnings("all")
	public static void main(String[] args) throws IOException {
		ServerSocket serverSocket = new ServerSocket(12000);
		System.out.println("服务器等待连接。。。。");
		while (true) {
			new Thread(() -> {
				try {
					Socket socket = serverSocket.accept();
					String ip = socket.getInetAddress().getHostAddress();
					System.out.println("服务器收到来自：" + ip + "的连接....");
					new Thread(() -> {
						try {
							byte[] b = new byte[1024];
							InputStream input = socket.getInputStream();
							System.out.println("服务等待接收内容");
							int read = input.read(b);
							System.out.println("服务器收到内容：" + new String(b, 0, read));
						} catch (IOException e) {
							e.printStackTrace();
						}
					}).start();

				} catch (IOException e) {
					e.printStackTrace();
				}

			}).start();
		}

	}
}

class Client1 {
	@SuppressWarnings("all")
	public static void main(String[] args) throws IOException {
		Socket socket = new Socket("127.0.0.1", 12000);
		while (true) {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
			String line = br.readLine();
			pw.println(line);
		}
	}
}
